/**
 * 具体门店的模型
 */

'use strict';

var mongodb = require('../mongodb.js');
var Schema = mongodb.Schema;
var ObjectId = Schema.Types.ObjectId;

var ModelSchema = new Schema({
    name: String,
    userId: ObjectId,

    // online(=available,  offline,  auditReject, auditWaiting, undone(tmp save)
    status: {type: String, 'default': 'auditWaiting'},
    address: String,
    telephone: String,
    description: String, // 描述,介绍

    imgUrl: [String], // ---图片url
    webUrl: String, //店铺的页面

    // 地区 & loc
    districts: [{_id: ObjectId, pId: ObjectId, name: String}],
    loc: {
        'type': {type: String, enum: "Point", 'default': "Point"},
        coordinates: {type: [Number], 'default': [0, 0]}
    },
    // ---种类
    cates: [{type: String, ref: 'category'}],

    createdAt: Date,
    updatedAt: {type: Date, 'default': Date.now()},

    extraInfo: String
});

ModelSchema.pre('save', function (next) {
    if (!this.createdAt) {
        this.createdAt = new Date();
    }
    next();
});

// 指定collection name，默认是复数
var shop = mongodb.model('shop', ModelSchema, 'shop');
exports = module.exports = shop;